﻿using System;
using System.Web.Mvc;
using GBFinance.Web.Models;
using HiLand.Framework.BusinessCore;
using HiLand.Framework.BusinessCore.BLL;
using HiLand.Utility.Entity;
using HiLand.Utility.Enums;
using HiLand.Utility.Paging;
using Webdiyer.WebControls.Mvc;
using HiLand.Utility.Data;
using HiLand.Utility.Web;
using HiLand.Utility4.MVC.Data;

namespace GBFinance.Web.Areas.Manage.Controllers
{
    /// <summary>
    /// 代理商功能
    /// </summary>
    public class BrokerController : Controller
    {
        /// <summary>
        /// 代理商列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Index(int id = 1)
        {
            int pageIndex = id;
            int pageSize = Miscs.PageSizeManage;
            int startIndex = (pageIndex - 1) * pageSize + 1;
            string whereClause = string.Format(" UserType= {0}  ", (int)UserTypes.Broker);
            string orderClause = "UserID DESC";

            string queryCondition = GetQueryCondition();
            if (string.IsNullOrWhiteSpace(queryCondition) == false)
            {
                whereClause += " AND " + queryCondition;
            }

            string queryName = RequestHelper.GetValue("queryConditionName");
            string queryValue = RequestHelper.GetValue("queryConditionValue");
            this.PassParam("queryName", queryName);
            this.PassParam("queryValue", queryValue);

            PagedEntityCollection<BusinessUser> entityList = BusinessUserBLL.GetPagedCollection(startIndex, pageSize, whereClause, orderClause);
            PagedList<BusinessUser> pagedList = new PagedList<BusinessUser>(entityList.Records, entityList.PageIndex, entityList.PageSize, entityList.TotalCount);
            return View(pagedList);
        }

        /// <summary>
        /// 设置用户状态为不可使用
        /// </summary>
        /// <param name="userGuid"></param>
        /// <returns></returns>
        public ActionResult SetBrokerStatusToUnnormal(Guid userGuid)
        {
            return SetBrokerStatus(userGuid, UserStatuses.Deleted);
        }

        /// <summary>
        /// 设置用户状态为停止使用
        /// </summary>
        /// <param name="userGuid"></param>
        /// <returns></returns>
        public ActionResult SetBrokerStatusToStop(Guid userGuid)
        {
            return SetBrokerStatus(userGuid, UserStatuses.Stopped);
        }

        /// <summary>
        /// 设置用户状态为正常使用
        /// </summary>
        /// <param name="userGuid"></param>
        /// <returns></returns>
        public ActionResult SetBrokerStatusToNormal(Guid userGuid)
        {
            return SetBrokerStatus(userGuid, UserStatuses.Normal);
        }

        /// <summary>
        /// 设置用户状态
        /// </summary>
        /// <param name="userGuid"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private ActionResult SetBrokerStatus(Guid userGuid, UserStatuses status)
        {
            LogicStatusInfo statusInfo = new LogicStatusInfo();
            statusInfo.IsSuccessful = BusinessUserBLL.SetUserStatus(userGuid, status);
            return Json(statusInfo);
        }

        #region 辅助方法
        /// <summary>
        /// 获取用户输入的查询条件
        /// </summary>
        /// <returns></returns>
        private string GetQueryCondition()
        {
            string result = string.Empty;
            string queryName = RequestHelper.GetValue("queryConditionName");
            string queryValue = RequestHelper.GetValue("queryConditionValue");

            switch (queryName)
            {
                case "CustomerNo":
                    if (string.IsNullOrWhiteSpace(queryValue) == false)
                    {
                        int customNo = Converter.TryToInt32(queryValue, 0);
                        if (customNo != 0)
                        {
                            result = string.Format(" UserID={0} ", customNo);
                        }
                    }
                    break;
                case "Email":
                    if (string.IsNullOrWhiteSpace(queryValue) == false)
                    {
                        result = string.Format(" UserEmail like '%{0}%'  ", SQLInjectionHelper.GetSafeSqlBeforeSave(queryValue));
                    }
                    break;
                case "FirstName":
                    if (string.IsNullOrWhiteSpace(queryValue) == false)
                    {
                        result = string.Format(" UserNameFirst like '%{0}%'  ", SQLInjectionHelper.GetSafeSqlBeforeSave(queryValue));
                    }
                    break;
                case "LastName":
                    if (string.IsNullOrWhiteSpace(queryValue) == false)
                    {
                        result = string.Format(" UserNameLast like '%{0}%'  ", SQLInjectionHelper.GetSafeSqlBeforeSave(queryValue));
                    }
                    break;
                case "PhoneNumber":
                    if (string.IsNullOrWhiteSpace(queryValue) == false)
                    {
                        result = string.Format(" WorkTelphone like '%{0}%'  ", SQLInjectionHelper.GetSafeSqlBeforeSave(queryValue));
                    }
                    break;
                case "Stopped":
                    result = string.Format(" UserStatus={0} ", (int)UserStatuses.Stopped);
                    break;
                case "Deleted":
                    result = string.Format(" UserStatus={0} ", (int)UserStatuses.Deleted);
                    break;
                default:
                    result = string.Empty;
                    break;
            }

            if (queryName != "Deleted" && queryName != "Stopped")
            {
                if (string.IsNullOrWhiteSpace(result))
                {
                    result = " 1=1 ";
                }

                result += string.Format(" AND UserStatus={0} ", (int)UserStatuses.Normal);
            }

            return result;
        }
        #endregion
    }
}
